import 'package:flutter/material.dart';
import 'package:fluttertest/models/route_model.dart';
import 'package:fluttertest/models/type_map.dart';
import '../../models/delivery_item.dart';

class AddItemDialog extends StatefulWidget {
  final List<TypeMap> allType;
  final List<TypeMap> allUnit;
  final Sequence sequence;
  final Function(DeliveryItem) onAdd;

  const AddItemDialog({
    super.key,
    required this.onAdd,
    required this.allType,
    required this.allUnit,
    required this.sequence
  });

  @override
  State<AddItemDialog> createState() => _AddItemDialogState();
}

class _AddItemDialogState extends State<AddItemDialog> {
  final _formKey = GlobalKey<FormState>();
  final _typeController = TextEditingController();
  final _quantityController = TextEditingController();
  final _unitController = TextEditingController();
  final _remarkController = TextEditingController();
  TypeMap? selectType ;
  TypeMap? selectUnit ;


  @override
  void dispose() {
    _typeController.dispose();
    _quantityController.dispose();
    _unitController.dispose();
    _remarkController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('添加签收项'),
      content: Form(
        key: _formKey,
        child: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Align(
                  alignment: Alignment.centerLeft,
                  child:
                  DropdownButtonFormField<TypeMap>(
                      hint: const Text("请选择类型"),
                      items: widget.allType.map((TypeMap e)=>DropdownMenuItem<TypeMap>(value: e, child: Text(e.typeName))).toList(),
                      value: selectType,
                      onChanged: (TypeMap? newValue){
                        setState(() {
                          selectType = newValue!;
                        });
                      },
                    validator: (TypeMap? value){
                      if(value==null){
                        return "请选择类型";
                      }
                    },
                  )),
              const SizedBox(height: 16),
              TextFormField(
                controller: _quantityController,
                decoration: const InputDecoration(
                  labelText: '实收数量',
                ),
                keyboardType: TextInputType.number,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请输入数量';
                  }
                  if (int.tryParse(value) == null) {
                    return '请输入有效的数字';
                  }
                  return null;
                },
              ),
              const SizedBox(height: 16),
              // TextFormField(
              //   controller: _unitController,
              //   decoration: const InputDecoration(
              //     labelText: '单位',
              //   ),
              //   validator: (value) {
              //     if (value == null || value.isEmpty) {
              //       return '请输入单位';
              //     }
              //     return null;
              //   },
              // ),
              Align(
                  alignment: Alignment.centerLeft,
                  child:
                  DropdownButtonFormField<TypeMap>(
                  hint: const Text("请选择单位"),
                  items: widget.allUnit.map((TypeMap e)=>DropdownMenuItem<TypeMap>(value: e, child: Text(e.typeName))).toList(),
                  value: selectUnit,
                  onChanged: (TypeMap? newValue){
                    setState(() {
                      selectUnit = newValue!;
                    });
                  },
                    validator: (TypeMap? value){
                      if(value==null){
                        return "请选择单位";
                      }
                    },
              )),
              const SizedBox(height: 16),
              TextFormField(
                controller: _remarkController,
                decoration: const InputDecoration(
                  labelText: '备注（选填）',
                ),
                maxLines: 2,
              ),
            ],
          ),
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('取消'),
        ),
        ElevatedButton(
          onPressed: () {
            if (_formKey.currentState!.validate()) {
              widget.onAdd(DeliveryItem(
                  itemcode: selectType!.typeId,
                  actualamount: int.parse(_quantityController.text),
                  shopCode: widget.sequence.storeNumber,
                  shopName: widget.sequence.storeName,
                  reason: selectUnit!.typeName,
                  comment: _remarkController.text,
                  createDate: DateTime.now().toString(),
                  sequenceId: widget.sequence.id,
                  waybillNumber: widget.sequence.waybillNumber
              ));
              Navigator.pop(context);
            }
          },
          child: const Text('添加'),
        ),
      ],
    );
  }
} 